// import{comboParameter} from '@/common/comboParameter.js';
import utils from '@/common/utils.js';
import store from '@/store';

export const fetch=(url,data={},method='POST',external)=>{
	let baseUrl='';
	if(external){
		baseUrl=store.state.marketUrl
	}else{
		baseUrl=store.state.baseUrl
	}
	let header={};
	if(store.state.token){
		header.token=store.state.token
	}
	header.local = uni.getLocale()
	let options={
		url:baseUrl+url,//开发者服务器接口地址
		data,
		method,//请求类型
		header,
		timeout:15000,//超时时间，单位 ms
	};
	return new Promise((resolve,reject)=>{
		uni.request({
			...options,
			success(res) {
				console.log(res.statusCode)
				console.log('成功')
				if(res.data.code==1){
					resolve(res.data)
				}else if(res.data.code==403){
					uni.reLaunch({
						url:'/pages/Login/Login'
					})
					// uni.showModal({
					// 	title:'提示',
					// 	content:res.data.errMsg,
					// 	showCancel:false,
					// 	success(r) {
					// 		if(r.confirm){
					// 			uni.reLaunch({
					// 				url:'/pages/base/login/login'
					// 			})
					// 		}
					// 	}
					// })
				}else if(res.statusCode == 503){
					console.log(res)
					reject(res)
					// utils.toast('网络错误')
					uni.hideLoading();
				}else{
					console.log(res)
					let local = uni.getLocale()
					let txt = '请求超时'
					if(local == 'zh-Hant'){
						txt = '請求超時'
					}else if(local == 'en'){
						txt = 'request timeout'
					}
					utils.toast(res.data.errMsg || txt)
					reject(res.data.errMsg)
					uni.hideLoading();
				}
			},
			fail(err){
				let local = uni.getLocale()
				let txt = '网络错误'
				if(local == 'zh-Hant'){
					txt = '網絡錯誤'
				}else if(local == 'en'){
					txt = 'network error'
				}
				console.log(err)
				reject(err)
				utils.toast(txt)
				uni.hideLoading();
			},
		})
	})
}
